﻿function Infra_Department() {

	// 表格Store
	var gridStoreDep = new Ext.data.Store({
		url: "/InfraDepartment/GetPlist",
		remoteSort: true,
		reader: new Ext.data.JsonReader({
			root: 'data',
			totalProperty: 'total',
			id: 'Id'
		}, [{
			name: 'Id',
			type: 'string'
		}, {
			name: 'CreateTime',
			type: 'date'
		}, {
			name: 'Name',
			type: 'string'
		}, {
			name: 'Code',
			type: 'string'
		}, {
			name: 'InputCode1',
			type: 'string'
		}, {
			name: 'InputCode2',
			type: 'string'
		}, {
			name: 'InputCode3',
			type: 'string'
		}, {
			name: 'Description',
			type: 'string'
		}]),
		listeners: {
			'update': function (thiz, record, operation) {
				var model = record.data;
				if (operation == Ext.data.Record.EDIT
								&& !record.getChanges().Id) {
					Ext.Ajax.request({
						url: '/InfraDepartment/SaveOrUpdate',
						params: model,
						success: function (response, opts) {
							var result = Ext.util.JSON.decode(response.responseText);
							if (result.success) {
								if (!record.get('Id'))
									record.set('Id', result.id);
								thiz.commitChanges();
							} else {
								JsHelper.ShowError(result.msg);
								thiz.rejectChanges();
							}
						}
					});
				}
			}
		}
	});
	gridStoreDep.setDefaultSort('CreateTime', 'DESC');

	// 定义Editor
	var gridEditor = new Ext.ux.grid.RowEditor();
	// 定义filters
	var filters = new Ext.ux.grid.GridFilters({
		encode: true,
		filters: [{
			type: 'string',
			dataIndex: 'Name'
		}, {
			type: 'string',
			dataIndex: 'Code'
		}]
	});
	// 定义表格列
	var sm = new Ext.grid.CheckboxSelectionModel();

	var DepSM = new Ext.grid.CheckboxSelectionModel({
		singleSelect: true,
		listeners: {
			'rowselect': function (sm, rowIndex, r) {
				//if (storeUpdateFlag)
				var deptCode = sm.getSelected().get("Code");
				gridStoreUser.removeAll();
				gridStoreUser.baseParams["deptCode"] = deptCode;
				gridStoreUser.load({
					params: {
						start: 0,
						limit: ALL_PAGESIZE_SETTING
					}
				})
			},
			'rowdeselect': function (sm, rowIndex, r) {
				//remove all.
				gridStoreUser.removeAll();
			}
		}
	});

	var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
	//sm, 
		{
		header: "机构名称",
		dataIndex: 'Name',
		width: 80,
		sortable: true,
		updateEdit: false,
		editor: {
			xtype: 'textfield',
			allowBlank: false,
			name: 'Name',
			plugins: [Ext.ux.plugins.RemoteValidator],
			rvOptions: {
				url: '/InfraDepartment/IsFieldExist',
				params: {
					'FieldName': 'Name',
					'Id': "-1"
				}
			}
		}
	}, {
		header: "代码",
		dataIndex: 'Code',
		sortable: true,
		updateEdit: false,
		width: 40,
		editor: {
			xtype: 'textfield',
			allowBlank: false,
			name: 'Code',
			plugins: [Ext.ux.plugins.RemoteValidator],
			rvOptions: {
				url: '/InfraDepartment/IsFieldExist',
				params: {
					'FieldName': 'Code',
					'Id': "-1"
				}
			}
		}
	}, {
		header: "助记码1",
		dataIndex: 'InputCode1',
		sortable: true,
		width: 70,
		editor: {
			xtype: 'textfield'
		}
	}, {
		header: "描述",
		dataIndex: 'Description',
		sortable: true,
		editor: {
			xtype: 'textfield'
		}
	}]);
	// 定义表格工具栏
	var tbar = [{
		xtype: "tbbutton",
		minWidth: 80,
		ref: '../addBtn',
		text: "添加项",
		cls: "x-btn-text-icon",
		icon: "/Content/icons/add.png",
		handler: function (btn, e) {
			gridEditor.stopEditing();
			gridStoreDep.insert(0, new gridStoreDep.recordType);
			DepGrid.getView().refresh();
			DepGrid.getSelectionModel().selectRow(0);
			gridEditor.startEditing(0);
		}
	}, "-", {
		xtype: "tbbutton",
		ref: '../delBtn',
		minWidth: 80,
		disabled: true,
		text: "删除项",
		cls: "x-btn-text-icon",
		icon: "/Content/icons/delete.png",
		handler: function (btn, e) {
			JsHelper.DelConfirm(function (btn) {
				if (btn == 'yes') {
					var records = DepGrid.getSelectionModel().getSelections();
					for (var i = 0; i < records.length; i++) {
						Ext.Ajax.request({
							url: '/InfraDepartment/Delete',
							params: {
								'Id': records[i].get("Id")
							},
							success: function (response, opts) {
								var result = Ext.util.JSON
													.decode(response.responseText);
								if (result.success == false) {
									JsHelper.ShowError(result.msg);
								} else {
									var paramsId = opts.params.Id;
									var r = gridStoreDep.getAt(gridStoreDep.find("Id", paramsId));
									if (Ext.isDefined(r)) {
										gridStoreDep.remove(r);
									}
								}
							}
						});
					}
				}
			});
		}
	}, "-", {
		xtype: "tbbutton",
		minWidth: 80,
		text: "双击修改项",
		cls: "x-btn-text-icon",
		icon: "/Content/icons/pencil.png",
		disabled: true
	}];

	// 分页工具栏
	var bbar = new Ext.PagingToolbar({
		displayInfo: true,
		emptyMsg: "没有数据显示",
		displayMsg: "{0}到{1}条，共{2}条",
		store: gridStoreDep,
		pageSize: ALL_PAGESIZE_SETTING,
		plugins: [filters]
	})
	// 定义表格
	var DepGrid = new Ext.grid.GridPanel({
		//region: "west",
		//width : 400,
		sm: DepSM,
		cm: cm,
		enableColumnMove: false,
		plugins: [gridEditor, filters],
		stripeRows: true,
		frame: true,
		border: true,
		//layout: "fit",
		store: gridStoreDep,
		loadMask: true,
		tbar: tbar,
		bbar: bbar
	});
	DepGrid.getSelectionModel().on('selectionchange', function (sm) {
		DepGrid.delBtn.setDisabled(sm.getCount() < 1);
	});

	// 注意 在ExtTabDoLayout之后Load
	gridStoreDep.load({
		params: {
			start: 0,
			limit: ALL_PAGESIZE_SETTING
		}
	});

	DepGrid.region = 'west';
	DepGrid.width = '350';

	// 用户清单
	var gridStoreUser = Infrastructure.UserRoleDept.UserRoleDeptStore();
	gridStoreUser.setDefaultSort('Person.Code', 'ASC');
	var UserRoleDeptGrid = Infrastructure.UserRoleDept.UserRoleDeptGrid();
	UserRoleDeptGrid.region = 'center';

	// 创建框架
	var main = new Ext.Panel({
		border: false,
		layout: 'border',
		//items: [DepGrid, UserGrid]
		items: [DepGrid, UserRoleDeptGrid]
	});

	// 加载
	JsHelper.ExtTabDoLayout(main);
}

//
Infra_Department();